package com.dmgameclient.adapter;

import java.util.List;

import com.dmgameclient.R;
import com.dmgameclient.beans.Comment;
import com.dmgameclient.common.AdapterImageUtils;
import com.dmgameclient.common.URLHelper;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CommentsListViewAdapter extends BaseAdapter {

	private static final String TAG = "CommentsListViewAdapter";

	private Context context;
	private List<Comment> list;
	private int itemViewResource;
	private AdapterImageUtils adapterImageUtils;

	class ItemHolder {
		public TextView userName;
		public TextView commentBody;
		public TextView floor;
		public ImageView headOverView;
	}

	public CommentsListViewAdapter(Context context, int itemViewResource,
			List<Comment> list, View view) {
		this.context = context;
		this.itemViewResource = itemViewResource;
		this.list = list;
		adapterImageUtils = new AdapterImageUtils(context, view,
				R.drawable.default_image, 110, 90);
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int arg0) {
		return list.get(arg0);
	}

	public long getItemId(int arg0) {
		return arg0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		ItemHolder itemHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					this.itemViewResource, null);
			itemHolder = new ItemHolder();

			itemHolder.userName = (TextView) convertView
					.findViewById(R.id.userName);
			itemHolder.floor = (TextView) convertView.findViewById(R.id.floor);
			itemHolder.commentBody = (TextView) convertView
					.findViewById(R.id.commentBody);
			itemHolder.headOverView = (ImageView) convertView
					.findViewById(R.id.headOverView);

			convertView.setTag(itemHolder);

		} else {
			itemHolder = (ItemHolder) convertView.getTag();
		}

		// 适配文本信息
		Comment comment = list.get(position);
		itemHolder.userName.setText(comment.getUsername());
		itemHolder.floor.setText(comment.getFloor());
		itemHolder.commentBody.setText(comment.getMsg());
		
		return convertView;
	}
}